﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;

namespace ESWebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CaptchaControl runat=server></{0}:CaptchaControl>"), ValidationProperty("Text")]
    public class CaptchaControl : System.Web.UI.WebControls.Image, INamingContainer
    {
        private int height = 40;
        private int width = 155;
        private CaptchaImage _captcha;
        private string guid = "CAPTCHAString";
        
        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }
        public override string ImageUrl
        {
            get
            {
                if (!DesignMode)
                {
                    
                    return string.Format("{0}?guid={1}", Page.Request.RawUrl, "FD8323442342fsdfeSEfe");
                }
                else
                    return CreateTempImage();
            }
        }
        public string Text
        {
            get
            {
                if (!DesignMode)
                {
                    if (Page.Session[guid] == null)
                        throw new NullReferenceException("The captcha image was not generated");
                    return ((CaptchaImage) Page.Session[guid]).Text;
                }
                else
                    return string.Empty;
            }
        }

        private string CreateTempImage()
        {
            string path = System.IO.Path.GetTempPath();
            string fileName = "captcha_temp_image.jpg";
            Stream stream = File.Create(path + fileName);
            RenderCaptcha(stream);
            return path + fileName;
        }

        private void RenderCaptcha(Stream stream)
        {
            _captcha = new CaptchaImage();
            _captcha.Height = height;
            _captcha.Width = width;
            Bitmap b = _captcha.RenderImage();
            b.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            b.Dispose();
            stream.Close();
        }

        protected override void OnInit(System.EventArgs e)
        {
            if (!DesignMode)
                if (!string.IsNullOrEmpty(Page.Request.QueryString["guid"]))
                    RenderCaptcha();
            base.OnInit(e);
        }

        private void RenderCaptcha()
        {
            Page.Response.Clear();
            RenderCaptcha(Page.Response.OutputStream);
            Page.Session[guid] = _captcha;
            Page.Response.ContentType = "image/jpeg";
            Page.Response.StatusCode = 200;
            Page.Response.End();
        }
    }
}
